package com.jiang.jixiang.ui;

import android.content.Intent;
import android.os.Bundle;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.databinding.ActivityMorefunctionBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.tools.PubDiaUtils;
import com.ws.universal.tools.Constants;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.eventbus.BaseBusData;
import com.ws.universal.tools.eventbus.BusCode;
import com.ws.universal.tools.eventbus.EventBusUtil;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MoreFunctionActivity extends BaseActivity implements NetWorkListener {
    private ActivityMorefunctionBinding binding;
    //获取协议
    private int agreement = 0;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityMorefunctionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void initView() {
        binding.inTitle.titleTextTv.setText(getText(R.string.setting));
        binding.inTitle.titleLeftBtn.setOnClickListener(v -> {
            onBackPressed();
        });
        //服务内容
        binding.RlServise.setOnClickListener(view -> {

            loadAgreement(0);
        });
        //用户协议
        binding.RlPrivacy.setOnClickListener(view -> {

            loadAgreement(1);
        });

        //联系方式
        binding.rlContact.setOnClickListener(view -> {

            pageJump(PwdChangeActivity.class);
        });


        //修改语言
        binding.rlLanguage.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), LanguageActivity.class);
            intent.putExtra("type", 2);
            startActivity(intent);
        });

        //注销账户
        binding.btnCancelAccount.setOnClickListener(view -> {
            showProgressDialog(false);
            //okHttpModel.post(ApiUrl.cancelAccount_Api, null, ApiUrl.cancelAccount_Api_ID, this);


        });

        //退出登录
        binding.btnExit.setOnClickListener(view -> {
            CacheUtils.setBoolean(Constants.isLogin, false);
            CacheUtils.setString(Constants.token, "");
            PubDiaUtils.getInstance().clearUserInfo();
            //ManageActivity.removeActivity(UserCenterFragment.class.getSimpleName());
            BaseBusData baseBusData = new BaseBusData(BusCode.LogOut);
            EventBusUtil.postEvent(baseBusData);
            finish();


        });
    }

    private void loadAgreement(int agreement) {
        this.agreement = agreement;
        Map<String, String> params = new HashMap<>();
        if (agreement == 0) {
            params.put("type", "user");

        } else if (agreement == 1) {
            params.put("type", "privacy");
        }

        okHttpModel.post(ApiUrl.agreement_Api, params, ApiUrl.agreement_Api_ID, this);
    }


    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.agreement_Api_ID:
                Bundle bundle = new Bundle();
                if (agreement == 0) {
                    bundle.putString("title", getString(R.string.service_agreement));
                } else if (agreement == 1) {
                    bundle.putString("title", getResources().getString(R.string.privacy_policy));
                }
                bundle.putString("loadH5Url", data.optString("content"));
                pageJump(AllHtmlActivity.class, bundle);
                break;

            /*case ApiUrl.cancelAccount_Api_ID:
                CacheUtils.setBoolean(Constants.isLogin, false);
                CacheUtils.setString(Constants.token, "");
                PubDiaUtils.getInstance().clearUserInfo();
                BaseBusData baseBusData = new BaseBusData(BusCode.LogOut);
                EventBusUtil.postEvent(baseBusData);
                onBackPressed();
                break;*/

        }
    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }
}
